<template>
  <div class="system-log-container">
    <div class="spaceForm">
      <el-form :inline="true" :model="queryForm">
        <el-form-item>
          <el-select v-model="type" placeholder="类型" @change="queryData">
            <el-option label="业主" value="owner" />
            <el-option label="租客" value="rent" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="queryForm.group_id"
            :clearable="true"
            placeholder="分组"
            @change="queryData"
          >
            <el-option
              v-for="item in group"
              :key="item.id"
              :label="item.group_name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="queryForm.bill_type"
            :clearable="true"
            placeholder="收支"
            @change="queryData"
          >
            <el-option label="收入" value="income" />
            <el-option label="支出" value="payed" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-date-picker
            v-model="queryForm.date"
            end-placeholder="结束日期"
            format="yyyy-MM-dd"
            start-placeholder="开始日期"
            type="daterange"
            value-format="yyyy-MM-dd"
            @change="changeDate"
          />
        </el-form-item>
        <el-form-item>
          <el-input
            v-model.trim="queryForm.keyword"
            clearable
            placeholder="请输入关键词"
            @keyup.enter.native="queryData"
            @clear="queryData"
          />
        </el-form-item>
        <el-button icon="el-icon-search" type="primary" @click="queryData">
          查询
        </el-button>
      </el-form>
      <el-form>
        <el-button @click="getExcel">导出表格</el-button>
      </el-form>
    </div>
    <div class="totalMoney">
      <div v-if="income != 0" class="income">
        收入
        <span>{{ income }}</span>
      </div>
      <div v-if="payed != 0" class="payed">
        支出
        <span>{{ payed }}</span>
      </div>
    </div>
    <el-table v-loading="listLoading" :data="list">
      <el-table-column
        label="费用名称"
        prop="bill_title"
        show-overflow-tooltip
      />
      <el-table-column
        v-if="type == 'rent'"
        label="姓名"
        prop="customer_name"
        show-overflow-tooltip
      />
      <el-table-column
        v-else-if="type == 'owner'"
        label="姓名"
        prop="user_name"
        show-overflow-tooltip
      />
      <el-table-column
        v-if="type == 'rent'"
        label="联系方式"
        prop="customer_mobile"
        show-overflow-tooltip
      />
      <el-table-column
        v-else-if="type == 'owner'"
        label="联系方式"
        prop="user_mobile"
        show-overflow-tooltip
      />
      <el-table-column label="地址" prop="room_address" show-overflow-tooltip />
      <el-table-column label="分组" prop="group_name" show-overflow-tooltip />
      <el-table-column
        label="支付方式"
        prop="pay_type_name"
        show-overflow-tooltip
      />
      <el-table-column label="金额" prop="bill_money">
        <template #default="{ row }">
          <div v-if="type == 'rent'">
            <div v-if="row.bill_type == 'income'">+{{ row.bill_money }}元</div>
            <div v-else-if="row.bill_type == 'payed'">
              {{ row.bill_money }}元
            </div>
          </div>
          <div v-else-if="type == 'owner'">
            <div v-if="row.bill_type == 'income'">+{{ row.bill_money }}元</div>
            <div v-else-if="row.bill_type == 'payed'">
              -{{ row.bill_money }}元
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="日期" prop="bill_date" show-overflow-tooltip />
      <el-table-column label="图片/备注" width="100">
        <template #default="{ row }">
          <div class="iconMore">
            <el-tooltip
              class="item"
              content="查看图片"
              effect="dark"
              placement="top"
            >
              <span>
                <vab-icon
                  v-if="row.bill_images != ''"
                  class="icon"
                  icon="image-2-line"
                  @click="lookImage(row)"
                />
              </span>
            </el-tooltip>
            <el-tooltip
              class="item"
              content="查看备注"
              effect="dark"
              placement="top"
            >
              <span>
                <vab-icon
                  v-if="row.bill_desc != ''"
                  class="icon"
                  icon="file-list-line"
                  @click="lookDesc(row)"
                />
              </span>
            </el-tooltip>
          </div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="85">
        <template #default="{ row }">
          <el-dropdown
            @command="
              (value) => {
                payMenu(value, row)
              }
            "
          >
            <span class="el-dropdown-link payMenu">更多</span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="editImage">编辑图片</el-dropdown-item>
              <el-dropdown-item command="editDesc">编辑备注</el-dropdown-item>
              <el-dropdown-item command="deletepay">删除</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
      <template #empty>
        <el-image
          class="vab-data-empty"
          :src="require('@/assets/empty_images/data_empty.png')"
        />
      </template>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.page"
      :layout="layout"
      :page-size="queryForm.perpage"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <el-image-viewer
      v-if="imageViewer"
      :on-close="closeViewer"
      :url-list="imageviewerList"
    />
    <el-dialog
      append-to-body
      title="查看备注"
      :visible.sync="dialogDescVisible"
      width="500px"
      @close="closeDesc"
    >
      <div class="desc">{{ desc }}</div>
      <template #footer>
        <el-button type="primary" @click="closeDesc">确定</el-button>
      </template>
    </el-dialog>
    <bill-desc ref="billDesc" @fetch-data="queryData" />
    <bill-images ref="billImages" @fetch-data="queryData" />
  </div>
</template>

<script>
import { billList, deleteBill } from '@/api/bill'
import { billList as ownerBillList, deleteOwnerBill } from '@/api/ownerBill'
import { parseTime } from '@/utils/index'
import { getgroup } from '@/api/group'
import billDesc from '@/views/components/components/billDesc'
import billImages from '@/views/components/components/billImages'
import ElImageViewer from 'element-ui/packages/image/src/image-viewer'
export default {
  name: 'BillList',
  components: { billDesc, billImages, ElImageViewer },
  data() {
    return {
      type: 'rent',
      list: [],
      group: [],
      listLoading: true,
      imageviewerList: [],
      imageViewer: false,
      dialogDescVisible: false,
      desc: '',
      layout: 'total, sizes, prev, pager, next, jumper',
      total: 0,
      income: 0,
      payed: 0,
      queryForm: {
        keyword: '',
        group_id: '',
        bill_type: '',
        is_pay: 1,
        page: 1,
        perpage: 20,
        start_time: null,
        end_time: null,
      },
    }
  },
  created() {
    this.getGroup()
    this.fetchData()
  },
  methods: {
    changeDate(value) {
      if (value) {
        this.queryForm.start_time = value[0]
        this.queryForm.end_time = value[1]
      } else {
        this.queryForm.start_time = null
        this.queryForm.end_time = null
      }
      this.queryData()
    },
    handleSizeChange(val) {
      this.queryForm.perpage = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.queryForm.page = val
      this.fetchData()
    },
    queryData() {
      this.queryForm.page = 1
      this.fetchData()
    },
    lookImage(row) {
      this.imageviewerList = row.bill_images.split(',')
      this.imageViewer = true
    },
    closeViewer() {
      this.imageViewer = false
      this.imageviewerList = []
    },
    lookDesc(row) {
      this.desc = row.bill_desc
      this.dialogDescVisible = true
    },
    closeDesc() {
      this.dialogDescVisible = false
      this.desc = ''
    },
    payMenu(type, row) {
      console.log(type, row)
      if (type == 'editImage') {
        this.$refs['billImages'].showEdit(this.type, row)
      }
      if (type == 'editDesc') {
        this.$refs['billDesc'].showEdit(this.type, row)
      }
      if (type == 'deletepay') {
        this.$baseConfirm(
          '你确定要删除此账单吗，删除后，此账单将被永久清除',
          null,
          async () => {
            if (this.type == 'rent') {
              const { data } = await deleteBill({ bill_ids: String(row.id) })
              if (data.is_success) {
                this.$baseMessage(
                  '删除成功',
                  'success',
                  'vab-hey-message-success'
                )
                this.fetchData()
              }
            } else if (this.type == 'owner') {
              const { data } = await deleteOwnerBill({
                bill_ids: String(row.id),
              })
              if (data.is_success) {
                this.$baseMessage(
                  '删除成功',
                  'success',
                  'vab-hey-message-success'
                )
                this.fetchData()
              }
            }
          }
        )
      }
    },
    async fetchData() {
      var queryData = Object.assign({}, this.queryForm)
      if (this.type == 'rent') {
        this.listLoading = true
        const { data } = await billList(queryData)
        this.list = data.bill_data.items
        this.total = data.bill_data.total
        this.income = data.total_money_income
        this.payed = data.total_money_payed
        this.listLoading = false
      } else if (this.type == 'owner') {
        this.listLoading = true
        const { data } = await ownerBillList(queryData)
        this.list = data.bill_data.items
        this.total = data.bill_data.total
        this.income = data.total_money_income
        this.payed = data.total_money_payed
        this.listLoading = false
      }
    },
    async getGroup() {
      const { data } = await getgroup({})
      this.group = data
    },
    getExcel() {
      import('@/utils/excel').then((excel) => {
        const header = [
          'ID',
          '费用名称',
          '姓名',
          '联系方式',
          '地址',
          '分组',
          '支付方式',
          '收支类型',
          '金额',
          '时间',
          '备注',
        ]
        const filterVal = [
          'id',
          'bill_title',
          'customer_name',
          'customer_mobile',
          'room_address',
          'group_name',
          'pay_type_name',
          'bill_type_name',
          'bill_money',
          'bill_date',
          'bill_desc',
        ]
        const list = this.list
        const data = this.formatJson(filterVal, list)
        const filename = 'bill' + new Date().valueOf()
        excel.export_json_to_excel({
          header,
          filename,
          data,
        })
      })
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map((v) =>
        filterVal.map((j) => {
          if (j === 'timestamp') {
            return parseTime(v[j])
          } else {
            return v[j]
          }
        })
      )
    },
  },
}
</script>
<style lang="scss" scoped>
.spaceForm {
  .el-select {
    width: 130px;
  }
  .el-input {
    width: 130px;
  }
  .el-date-editor.el-input {
    width: 130px;
  }
  .daterange {
    margin-right: 10px;
    line-height: 32px;
  }
}
.totalMoney {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
  span {
    margin-right: 20px;
    font-size: 20px;
  }
  .income span {
    color: #1ecb6b;
  }
  .payed span {
    color: #ee5959;
  }
}
.iconMore {
  .icon {
    font-size: 20px;
    cursor: pointer;
    margin-right: 10px;
    color: #999;
    &:hover {
      color: #91b8ff;
    }
  }
}
.payMenu {
  color: #5187f7;
  cursor: pointer;
}
</style>
